using System;
using System.Windows;
using System.Windows.Controls;
namespace Microsoft.Phone.Controls
{
	internal sealed class InteractionHelper
	{
		private const double SequentialClickThresholdInMilliseconds = 500.0;
		private const double SequentialClickThresholdInPixelsSquared = 9.0;
		private IUpdateVisualState _updateVisualState;
		public Control Control
		{
			get;
			private set;
		}
		public bool IsFocused
		{
			get;
			private set;
		}
		public bool IsMouseOver
		{
			get;
			private set;
		}
		public bool IsReadOnly
		{
			get;
			private set;
		}
		public bool IsPressed
		{
			get;
			private set;
		}
		public InteractionHelper(Control control)
		{
			this.Control = control;
			this._updateVisualState = (control as IUpdateVisualState);
			control.add_Loaded(new RoutedEventHandler(this.OnLoaded));
			control.add_IsEnabledChanged(new DependencyPropertyChangedEventHandler(this.OnIsEnabledChanged));
		}
		private void UpdateVisualState(bool useTransitions)
		{
			if (this._updateVisualState != null)
			{
				this._updateVisualState.UpdateVisualState(useTransitions);
			}
		}
		public void UpdateVisualStateBase(bool useTransitions)
		{
			if (!this.Control.get_IsEnabled())
			{
				VisualStates.GoToState(this.Control, useTransitions, new string[]
				{
					"Disabled",
					"Normal"
				});
			}
			else
			{
				if (this.IsReadOnly)
				{
					VisualStates.GoToState(this.Control, useTransitions, new string[]
					{
						"ReadOnly",
						"Normal"
					});
				}
				else
				{
					if (this.IsPressed)
					{
						VisualStates.GoToState(this.Control, useTransitions, new string[]
						{
							"Pressed",
							"MouseOver",
							"Normal"
						});
					}
					else
					{
						if (this.IsMouseOver)
						{
							VisualStates.GoToState(this.Control, useTransitions, new string[]
							{
								"MouseOver",
								"Normal"
							});
						}
						else
						{
							VisualStates.GoToState(this.Control, useTransitions, new string[]
							{
								"Normal"
							});
						}
					}
				}
			}
			if (this.IsFocused)
			{
				VisualStates.GoToState(this.Control, useTransitions, new string[]
				{
					"Focused",
					"Unfocused"
				});
				return;
			}
			VisualStates.GoToState(this.Control, useTransitions, new string[]
			{
				"Unfocused"
			});
		}
		private void OnLoaded(object sender, RoutedEventArgs e)
		{
			this.UpdateVisualState(false);
		}
		private void OnIsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
		{
			if (!(bool)e.get_NewValue())
			{
				this.IsPressed = false;
				this.IsMouseOver = false;
				this.IsFocused = false;
			}
			this.UpdateVisualState(true);
		}
		public void OnIsReadOnlyChanged(bool value)
		{
			this.IsReadOnly = value;
			if (!value)
			{
				this.IsPressed = false;
				this.IsMouseOver = false;
				this.IsFocused = false;
			}
			this.UpdateVisualState(true);
		}
		public void OnApplyTemplateBase()
		{
			this.UpdateVisualState(false);
		}
	}
}
